package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.patrons.CombSkillUnlockInfo;
import com.yanqu.road.entity.config.vehicle.*;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class VehicleDaoImpl extends TempDao {

	public Map<Integer, VehicleInfo> getVehicleInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_vehicle";
		Map<Integer, VehicleInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				VehicleInfo data = new VehicleInfo();
				data.setId(rs.getInt("id"));
				data.setQuality(rs.getInt("quality"));
				data.setDefaultSkillCount(rs.getInt("defaultSkillCount"));
				data.setUpgradeConsumeCount(rs.getInt("upgradeConsumeCount"));
				data.setQualification(rs.getLong("qualification"));
				data.setAddQualification(rs.getLong("addQualification"));
				data.setRecycleReward(PropertyHelper.parseStringToProperty(rs.getString("recycleReward")));
				data.setStudyTypeList(StringUtils.stringToIntegerList(rs.getString("studyType"), "\\|"));
				data.setCanBreak(rs.getBoolean("canBreak"));
				data.setCanRecycle(rs.getBoolean("canReturn"));
				data.setCombId(rs.getInt("combId"));
				data.setBonePowerSkill(rs.getInt("bonePowerSkill"));
				dataMap.put(data.getId(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, VehicleBreak> getVehicleBreakMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_vehicle_break";
		Map<Integer, VehicleBreak> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				VehicleBreak data = new VehicleBreak();
				data.setBreakLv(rs.getInt("breakLv"));
				data.setCurrentMaxLv(rs.getInt("currentMaxLv"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setTotalConsume(PropertyHelper.parseStringToProperty(rs.getString("totalConsume")));

				dataMap.put(data.getBreakLv(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, Map<Integer, VehicleStudy>> getVehicleStudyMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_vehicle_study";
		Map<Integer, Map<Integer, VehicleStudy>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				VehicleStudy data = new VehicleStudy();
				data.setType(rs.getInt("type"));
				data.setStudyLv(rs.getInt("studyLv"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setTotalConsume(PropertyHelper.parseStringToProperty(rs.getString("totalConsume")));
				if(!dataMap.containsKey(data.getType())){
					dataMap.put(data.getType(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getType()).put(data.getStudyLv(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, VehicleSkillInfo> getVehicleSkillInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_vehicle_skill";
		Map<Integer, VehicleSkillInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				VehicleSkillInfo data = new VehicleSkillInfo();
				data.setSkillType(rs.getInt("skillType"));
				data.setMinValue(rs.getInt("minValue"));
				data.setMaxValue(rs.getInt("maxValue"));

				dataMap.put(data.getSkillType(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, VehicleComb> getVehicleCombMap() {
		Map<Integer, VehicleComb> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_patrons_comb where `type` = 3";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				VehicleComb data = new VehicleComb();
				data.setId(rs.getInt("id"));
				String skillStr = rs.getString("skillId");
				List<String> skillInfoList = StringUtils.stringToStringList(skillStr,"\\|");
				if(skillInfoList.size() > 1){
					Map<Integer,Integer> skillMap = new ConcurrentHashMap<>();
					for(String skillInfo : skillInfoList){
						List<Integer> infoList = StringUtils.stringToIntegerList(skillInfo,"=");
						skillMap.put(infoList.get(0),infoList.get(1));
					}
					data.setVehicleSkill(skillMap);
				}else{
					data.setSkillId(rs.getInt("skillId"));
				}

				Map<Integer, Map<Integer, CombSkillUnlockInfo>> patronsAdvancedSkillMap = PatronsDaoImpl.getICombSkillUnlockInfoMap(rs);

				data.setAdvancedSkillMap(patronsAdvancedSkillMap);

				data.setCombSkillId(rs.getInt("combSkillId"));

				data.setExtra(rs.getString("extra"));

				data.setCombList(StringUtils.stringToIntegerList(data.getExtra(),"\\|"));

				// 自身光环
				String skillId2Str = rs.getString("skillId2");
				List<String> selfSkillInfoList = StringUtils.stringToStringList(skillId2Str,"\\|");
				Map<Integer, List<Integer>> selfSkillMap = new ConcurrentHashMap<>();
				for(String skillInfo : selfSkillInfoList){
					List<Integer> infoList = StringUtils.stringToIntegerList(skillInfo,"=");
					if (infoList.size() < 2) {
						continue;
					}
					Integer vehicleId = infoList.get(0);
					Integer selfSkillId = infoList.get(1);
					List<Integer> orDefault = selfSkillMap.getOrDefault(vehicleId, new ArrayList<>());
					orDefault.add(selfSkillId);
					selfSkillMap.put(vehicleId, orDefault);
				}
				data.setVehicleSelfSkillMap(selfSkillMap);

				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

}
